<?php

/**
 *
 * @file searchbox.php
 * @brief Clase para generar un searchbox
 * @author DYR Technologies
 * @version V 1.1
 * @date Mon Oct 29 01:47:15 GMT 2007
 *
 *  Part of the CEFIRO dynamic web framework project
 *
 *  Copyright (C) 2007  Dyr technologies http://dyrtech.com.ve
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @class Clase searchbox
 */
if (!defined('CEFIRO')) require_once('../cefiro.php');
require_once(CEF_TEXTBOX);

/**
 * @brief Define la ruta a la hoja de estilos y javascript para el searchbox
 *
 */
define("SEARCHBOX_PATH",FULL_URL."searchbox/searchbox.php");
define("SEARCHBOX_JS",FULL_URL."searchbox/js/searchbox.js");
define("SEARCHBOX_CSS",FULL_URL."searchbox/css/searchbox.css");

class searchbox extends textbox {
	public $backend_script;
	public $backend_fcn;
	public $noresults;
	public $callback;
	public $minchars;
	public $delay;
	public $timeout;
	public $cache;

	private $index;
	private static $aOptions;

	public function searchbox($_name='__searchbox', $_label='', $_value='', $_readonly = false, $_opt = '', $_dom = ''){
	  $this->index = (isset(self::$aOptions) ? @count(self::$aOptions) : 0 );
		/**
		 * @brief El nombre del widget es el tipo con el numero de la instancia que representa o el nombre pasado por omision en caso contrario
		 */
		if($_name === "__searchbox")
  		$_name = "searchbox".$this->index;
		$this->textbox($_name,$_label,$_value,$_readonly,$_opt,$_dom);
  }

  public function setOptions($backend_script, $backend_fcn, $callback = '', $noresults = 'No encontrado', $minchars = 3, $delay = 500, $timeout = 2500, $cache = true){
    $this->backend_script = $backend_script;
    $this->backend_fcn = $backend_fcn;
  	$this->noresults = $noresults;
  	$this->callback = $callback;
  	$this->minchars = $minchars;
  	$this->delay = $delay;
  	$this->timeout = $timeout;
  	$this->cache = $cache;
  }

	/**
	 * @brief Asigna el searchbox a la variable $smarty para su renderizacion
	 */
	public function displaySearchbox(){
	  self::$aOptions[$this->index] = array(
	    'name' => $this->name,
      'backend_script' => $this->backend_script,
      'backend_fcn' => $this->backend_fcn,
      'noresults' => $this->noresults,
      'callback' => $this->callback,
      'minchars' => $this->minchars,
      'delay' => $this->delay,
      'timeout' => $this->timeout,
      'cache' => $this->cache);
		$this->display_textbox();
	}

	public static function getSearchboxJs(){
	  return SEARCHBOX_JS;
	}

	public static function getSearchboxCss(){
	  return SEARCHBOX_CSS;
	}

  public static function displaySearchboxHead($smartyVar = '') {
    global $smarty;
		$smarty->assign("searchboxcss",SEARCHBOX_CSS);
		$smarty->assign("sb_js",SEARCHBOX_JS);
   	$smarty->assign("sb_head",'true');
   	$smarty->assign("smartyVar",$smartyVar);
   	$smarty->template_dir = dirname(__FILE__).'/';
	  $head = $smarty->fetch('searchbox.tpl');
	  return $smartyVar == '' ? $head : $smarty->assign($smartyVar, $head);
  }

  public static function displaySearchboxJs($smartyVar = '') {
		if (isset(self::$aOptions)){
      global $smarty;
     	$smarty->assign("mOpts",self::$aOptions);
     	$smarty->assign("sb_head",'false');
     	$smarty->assign("sb_path",SEARCHBOX_PATH);
     	$smarty->template_dir = dirname(__FILE__).'/';
		  $js = $smarty->fetch('searchbox.tpl');
      return $smartyVar == '' ? $js : $smarty->assign( $smartyVar, $js );
		} else
		  return null;
  }

  public static function xmlResponse($backend_script, $backend_fcn, $backend_var) {
    @define('BACKEND_SCRIPT',ROOT.HOME.'/'.$backend_script);
    include_once(BACKEND_SCRIPT);
    $aResults = call_user_func($backend_fcn,$backend_var);
    header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
    header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
    header ("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
    header ("Pragma: no-cache"); // HTTP/1.0
    header("Content-type: text/xml; charset=utf-8");
    echo "<?xml version=\"1.0\" encoding=\"utf-8\" ?><results>";
    foreach ($aResults as $key => $value) {
      if (is_string($value))
    		echo "<rs id=\"$key\" >$value</rs>";
    	else
    		echo "<rs id=\"$key\" info=\"".$value[1]."\">".$value[0]."</rs>";
    }
    echo "</results>";
  }
};

if (isset($_GET['backend_script']) && isset($_GET['backend_fcn']) && isset($_GET['backend_var'])) {
  searchbox::xmlResponse($_GET['backend_script'],$_GET['backend_fcn'],$_GET['backend_var']);
}
?>